const http = require('http')
const fs = require('fs')
const path = require('path');
const server = http.createServer()
const port = 6666

server.listen(port, function() {
    console.log('server is listening ' + port)
})

generateExport()

// 生成 export.js 文件
function generateExport() {
    let content = ''
    travel(path.resolve('./libs'), function(pathname) {
        if (pathname.indexOf('export.js') === -1 && pathname.indexOf('namespace.js') === -1) {
            content += String(fs.readFileSync(pathname))
        }
    })
    new Promise((resolve) => {
        write(path.resolve('./export.js'), content, resolve)
    }).then(() => {
        // 引入测试文件
        require('./public/index.js')
    })


}
// 遍历目录
function travel(dir, callback) {
    fs.readdirSync(dir).forEach((file) => {
        var pathname = path.join(dir, file)
        if (fs.statSync(pathname).isDirectory()) {
            travel(pathname, callback)
        } else {
            callback(pathname)
        }
    })
}
// 写文件
function write(dir, content, resolve) {
    fs.writeFile(dir, content, function(err) {
        resolve()
        if (err) {
            return console.log('generate export.js error',err);
        }
        console.log("export.js is generated");
    });
}
